﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WpfTool.Models;

namespace WpfTool
{
    public class MainViewModel
    {
        public List<MenuItemModel> TreeList { get; set; }
        public ObservableCollection<PageItemModel> PageList { get; set; } = new ObservableCollection<PageItemModel>();
        public MainViewModel()
        {
            TreeList = GetMenuList();
            TreeList.Add(new MenuItemModel()
            {
                Header = "菜单管理",
                IconCode = "\ue605",
                TargetView = "XiTong.Menu",
                OpenViewCommand = new Command<MenuItemModel>(OpenView),
                Children = new List<MenuItemModel>()
            });
            TreeList.Add(new MenuItemModel()
            {
                Header = "表操作",
                IconCode = "\ue605",
                TargetView = "Db.BiaoCaoZuo",
                OpenViewCommand = new Command<MenuItemModel>(OpenView),
                Children = new List<MenuItemModel>()
            });
            TreeList.Add(new MenuItemModel()
            {
                Header = "组件示例",
                IconCode = "\ue605",
                TargetView = "ZhuJian.BiaoGe",
                OpenViewCommand = new Command<MenuItemModel>(OpenView)
            });
        }

        private List<MenuItemModel> GetMenuList()
        {
            List<WpfMenu> list = DbHelper.DbMssql.Queryable<WpfMenu>().ToList();

            return ToTreeMenu(list, Guid.Empty);
        }

        private List<MenuItemModel> ToTreeMenu(List<WpfMenu> list, Guid id)
        {
            List<MenuItemModel> result = [];
            var temp = list.Where(t => t.ParentId == id).ToList();
            foreach (WpfMenu item in temp)
            {
                string icon = System.Text.RegularExpressions.Regex.Unescape(string.Format("\\u{0}", item.IconCode));

                MenuItemModel model = new()
                {
                    Header = item.MenuName,
                    IconCode = icon,
                    TargetView = item.MenuUrl,
                    OpenViewCommand = new Command<MenuItemModel>(OpenView),
                    Children = new List<MenuItemModel>()
                };

                var child = ToTreeMenu(list, item.Id);
                if (child.Count > 0)
                {
                    model.Children = child;
                }

                result.Add(model);
            }
            return result;
        }

        private void OpenView(MenuItemModel menu)
        {
            var page = PageList.FirstOrDefault(p => p.Header == menu.Header);
            if (page == null)
            {
                Type type = Assembly.GetExecutingAssembly().GetType("WpfTool." + menu.TargetView);
                object p = Activator.CreateInstance(type);
                PageList.Add(new PageItemModel()
                {
                    IsSelected = true,
                    Header = menu.Header,
                    PageView = p,
                    CloseTabCommand = new Command<PageItemModel>(ClosePage)
                });
            }
            else
            {
                page.IsSelected = true;
            }
        }

        private void ClosePage(PageItemModel menu)
        {
            PageList.Remove(menu);
        }
    }
}
